package jpb.util.load;

import jpb.util.IndexUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by haol on 2017/4/17.
 */
public class UpExcelUtil {
        private Logger logger = LoggerFactory.getLogger(UpExcelUtil.class);


        public HashMap<String, Object> analyzeTitle(HSSFWorkbook workbook, String[] head) {
                HashMap<String, Object> hashMap = new HashMap<String, Object>();


                HSSFSheet sheet = workbook.getSheetAt(0);
                HSSFRow row;
                HSSFCell cell;
                String sheetName = sheet.getSheetName();
                logger.info("读取的work表单名为{}", sheetName);
                hashMap.put("oid", sheetName);

                cell = sheet.getRow(0).getCell(0);
                String oName = cell.getStringCellValue();
                logger.info("读取的work头标题为{}", oName);
                hashMap.put("oname", oName);
                row = sheet.getRow(1);
                int rowSize = row.getLastCellNum();

                List<String> rows = new ArrayList<String>();
                for (int i = 0; i < rowSize; i = i + head.length) {
                        cell = row.getCell(i);
                        rows.add(cell.getStringCellValue());
                }
                hashMap.put("deps", rows);
                return hashMap;


        }
        public HashMap<Integer, List<String[]>> analyzeContent(HSSFWorkbook workbook, String[] head,int groups) {
                return this.analyzeContent(workbook,head,3,groups);
        }

        public HashMap<Integer, List<String[]>> analyzeContent(HSSFWorkbook workbook, String[] head,int ro,int groups) {

                HashMap<Integer, List<String[]>> hashMap = new HashMap<Integer, List<String[]>>();

                HSSFSheet sheet = workbook.getSheetAt(0);

                HSSFRow row;
                HSSFCell cell;

                int rowNum =sheet.getLastRowNum();
                int index = IndexUtil.getINDEX("编号",head);
                int ro_ = ro;
                for (int co = 0;co<groups*head.length;co=co+head.length){
                        List<String[]> cons = new ArrayList<String[]>();

                        for (ro=ro_;ro<rowNum;ro++){
                                String[] value = new String[head.length];
                                for(int s = 0;s<head.length;s++){
                                        cell = sheet.getRow(ro).getCell(co+s);
                                        value[s]  = cell.getStringCellValue();
                                }
                              if (value[index].isEmpty()){
                                      break;
                              }
                                cons.add(value);
                        }
                        hashMap.put(co/head.length,cons);
                }
                return hashMap;
        }
        public String  getFileOid(HSSFWorkbook workbook) {


                HSSFSheet sheet = workbook.getSheetAt(0);
                HSSFCell cell;
                String sheetName = sheet.getSheetName();
                logger.info("读取的work表单名为{}", sheetName);

                cell = sheet.getRow(0).getCell(0);
                String oName = cell.getStringCellValue();
                return oName;

        }

}
